home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.000 / crossfir / crossfire-0.92.4.client / client.c < prev    next >
C/C++ Source or Header  |  1996-04-21  |  4KB  |  155 lines

  1. /* Client interface main routine.
  2.   * this file sets up a few global variables, connects to the server,
  3.   * tells it what kind of pictures it wants, adds the client and enters
  4.   * the main dispatch loop
  5.   *
  6.   * the main event loop (event_loop()) checks the tcp socket for input and
  7.   * then polls for x events.  This should be fixed since you can just block
  8.   * on both filedescriptors.
  9.   *
  10.   * The DoClient function recieves a message (an ArgList), unpacks it, and
  11.   * in a slow for loop dispatches the command to the right function through
  12.   * the commands table.   ArgLists are essentially like RPC things, only 
  13.   * they don't require going through RPCgen, and it's easy to get variable
  14.   * length lists.  They are just lists of longs, strings, characters, and 
  15.   * byte arrays that can be converted to a machine independent format
  16.  */
  17.  
  18. #include <client.h>
  19. /*
  20. #include <tcplib.h>
  21. */
  22.  
  23. /* actually declare the globals */
  24.  
  25. char *server="localhost",*client_libdir=NULL, *client_archfile=NULL;
  26. int port_num=EPORT;
  27. FILE *fpin,*fpout;
  28. int fdin, fdout, basenrofpixmaps, pending_images=0,maxfiledescriptor,
  29.     pending_archs=0;
  30. char init_load_image=FALSE, init_load_arch=FALSE, save_new_data=FALSE;
  31. Client_Player cpl;
  32. TcpSocket conns[2];
  33.  
  34. struct CmdMapping {
  35.   char *cmdname;
  36.   void (*cmdproc)(ArgList);
  37. };
  38.  
  39. void FooCmd(ArgList msg)
  40. {
  41.   printf("Foo: %s\n",ArgList_getString(msg,2));
  42. }
  43. void EatMeCmd(ArgList msg)
  44. {
  45. }
  46.  
  47. struct CmdMapping commands[] = {
  48.   { "map", MapCmd },
  49.   { "map_scroll", map_scrollCmd },
  50.   { "item", ItemCmd },
  51.   { "drawinfo", DrawInfoCmd },
  52.   { "stats", StatsCmd },
  53.   { "pixmap", PixMapCmd },
  54.   { "bitmap", BitMapCmd },
  55. /*  { "write_ch", write_chCmd },*/
  56.   { "version", VersionCmd },
  57.  
  58.   { "player", PlayerCmd },
  59.  
  60.   { "foo", FooCmd },
  61.   { "bar", EatMeCmd },
  62.  
  63.   { "addme_failed", PrintMsg },
  64.   { "addme_success", PrintMsg },
  65.  
  66.   { "query", handle_query},
  67. };
  68.  
  69. #define NCOMMANDS (sizeof(commands)/sizeof(struct CmdMapping))
  70.  
  71. void DoClient(TcpSocket conn)
  72. {
  73.   ArgList msg;
  74.   int i;
  75.   char *cmd;
  76.  
  77.   msg = ArgList_receive(conn);
  78.   if (ArgList_getLong(msg,0)!= STRINGCOMMAND) {
  79.     printf("Bad message from server (%ld)\n",ArgList_getLong(msg,0));
  80.     exit(1);
  81.   }
  82.   cmd = ArgList_getString(msg,1);
  83.   printf("Command:%s\n",cmd);
  84.   for(i=0;i < NCOMMANDS;i++) {
  85.     if (strcmp(cmd,commands[i].cmdname)==0) {
  86.       commands[i].cmdproc(msg);
  87.       break;
  88.     }
  89.   }
  90.   if (i == NCOMMANDS) {
  91.     printf("Bad command from server (%s)\n",cmd);
  92.     exit(1);
  93.   }
  94. }
  95.  
  96.  
  97. /* This is the loop that the client goes through once all the
  98.  * initialization is done.  Basically, it checks for input and
  99.  * processes X events (calls function to do that.)
  100.  * The time for command_loop is fairly arbitrary - it can be most
  101.  * any value.  If it is very low, however, as it will be doing a lot
  102.  * of checks to see if there is data instead of blocking on input.
  103.  *
  104.  * check_x_events takes all the events that are waiting.
  105.  */
  106.  
  107. void event_loop()
  108. {
  109.     while (1) {
  110.     WaitForInput(conns, 1, MAX_TIME/1000);
  111.     if (HasInput(conns[0]))
  112.         DoClient(conns[0]);
  113.     check_x_events();
  114.     }
  115.  
  116. }
  117.  
  118.  
  119. int main(int argc, char *argv[])
  120. {
  121.  
  122.  
  123.     /* This needs to be done first.  In addition to being quite quick,
  124.      * it also sets up some paths (client_libdir) that are needed by
  125.      * the other functions.
  126.      */
  127.  
  128.     init_client_vars();
  129.     
  130.     /* Call this very early.  It should parse all command
  131.      * line arguments and set the pertinent ones up in
  132.      * globals.  Also call it early so that if it can't set up
  133.      * the windowing system, we get an error before trying to
  134.      * to connect to the server.  And command line options will
  135.      * likely change on the server we connect to.
  136.      */
  137.     if (init_windows(argc, argv)) {    /* x11.c */
  138.     fprintf(stderr,"Failure to init windows.\n");
  139.     exit(1);
  140.     }
  141.     conns[0] = GetConnection(server, port_num);
  142.     SendVersion(conns[0]);
  143.     if (display_usebitmaps()) 
  144.     SendSetFaceMode(conns[0],1); /* Yeah! arbitrary constants! ;>*/
  145.     if (display_noimages())
  146.     SendSetFaceMode(conns[0],0);
  147.  
  148. /*    SendKeyConversion(conns[0]);*/
  149.     SendAddMe(conns[0]);
  150.  
  151.     event_loop();
  152.  
  153.     exit(0);
  154. }
  155.